package collection.mapTest;

import java.util.Iterator;
import java.util.TreeMap;

/**
 * 
 * 测试TreeMap，测试其排序功能
 * 所有的MAP与SET都是类似的功能
 * @author lcx
 *
 */
public class TreeMapTest {
	
	public static void main(String[] args) {
		TreeMap<String,Cache2> map=new TreeMap<String,Cache2>();
		Cache2 c1=new Cache2("1",1);
		Cache2 c2=new Cache2("2",2);
		Cache2 c3=new Cache2("3",3);
		map.put("1", c1);
		map.put("3", c3);
		map.put("2", c2);
		Iterator keys=map.keySet().iterator();
		while(keys.hasNext())
		{
			Object key= keys.next();
			Cache2 c=map.get(key);
			c.showInfo();
		}
	}
}

class Cache2 implements Comparable
{
	String id;//�൱������
	int val;
	
	public Cache2(String id,int val)
	{
		this.id=id;
		this.val=val;
	}
	
	public void setValue(int val)
	{
		this.val=val;
	}
	
	public void showInfo()
	{
		System.out.println("Cache ID:   "+id+"   Cache val:   "+val);
	}

	public int compareTo(Object o) {
		if(o instanceof Cache2)
		{
			Cache2 c=(Cache2) o;
			if(this.val>c.val)
				return -1;
			if(this.val<=c.val)
				return 1;
		}
		return 0;
	}
}